﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using IRongs.Wbt.Common.Auth;

namespace IRongs.Wbt.Desktop.Common
{
    /// <summary>
    /// 桌面版应用程序的用户信息
    /// </summary>
    [Serializable]
    public class DesktopProfile : ProfileBase
    {
        public DesktopProfile()
        {
        }

        public DesktopProfile(string userId, string screenName)
            : this()
        {
            this.UserId = userId;
            this.ScreenName = screenName;

        }

        private string _profileFile;
        /// <summary>
        /// Gets the profile file that stores the user's profiles including the request sender..
        /// </summary>
        /// <value>The profile file.</value>
        public string ProfileFile
        {
            get
            {
                if (string.IsNullOrEmpty(_profileFile))
                {
                    _profileFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format(@"{1}\{0}.dat", UserId, "users"));
                }

                return _profileFile;
            }
        }

        /// <summary>
        /// Saves this instance.
        /// </summary>
        public void Save()
        {
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();
                using (FileStream stream = new FileStream(ProfileFile, FileMode.Create))
                {
                    formatter.Serialize(stream, this);
                }
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// Gets the profile.
        /// </summary>
        /// <param name="profilePath">The profile path.</param>
        /// <returns></returns>
        public static DesktopProfile GetProfile(string profilePath)
        {
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();
                using (FileStream stream = new FileStream(profilePath, FileMode.Open))
                {
                    return formatter.Deserialize(stream) as DesktopProfile;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Gets the profiles for all the user's storage.
        /// </summary>
        /// <returns></returns>
        public static List<DesktopProfile> GetProfiles()
        {
            string directoryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "users");
            string[] files = Directory.GetFiles(directoryPath, "*.dat");

            List<DesktopProfile> profiles = new List<DesktopProfile>(10);
            foreach (string file in files)
            {
                string filePath = Path.Combine(directoryPath, file);
                DesktopProfile profile = GetProfile(filePath);
                if (profile != null)
                {
                    profiles.Add(profile);
                }
            }

            return profiles;
        }
    }
}
